<template>
  <a-modal :open="open" title="退药" centered :width="600" ok-text="保存" :confirm-loading="confirmLoading" @ok="onSave" @cancel="onCancel">
    <div class="flex justify-between items-center">
      <div class="w-[80%] yaop">{{ formatSate.mc }} / {{ formatSate.gg }} / {{ formatSate.cj }}</div>
      <div>
        <span>发药数量：</span><span>{{ formatSate.sl }}{{ formatSate.dw }}</span>
      </div>
    </div>
    <a-divider class="!my-4" />
    <a-form ref="formRef" class="" :model="formatSate" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="退药数量" class="w-1/2">
        <a-input v-model:value="formatSate.tysl" v-input:int placeholder="请输入">
          <template #suffix>
            <span class="text-slate-400">{{ formatSate.dw }}</span>
          </template>
        </a-input>
      </a-form-item>
      <a-form-item label="备注">
        <a-textarea v-model:value="formatSate.remark" :rows="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { message } from 'ant-design-vue'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  },
  reviewer: {
    type: String,
    default: () => ''
  }
})
const emit = defineEmits(['update:open', 'success'])
const formatSate = ref({})
const confirmLoading = ref(false)

const onSave = async () => {
  if (formatSate.value.tysl == null || formatSate.value.tysl === '') {
    message.error('退药数量不可为空')
    return
  }
  if (formatSate.value.tysl === 0 || formatSate.value.tysl === '0') {
    message.error('退药数量不可为0')
    return
  }
  if (formatSate.value.tysl > formatSate.value.sl) {
    message.error('退药数量不可大于原始数量')
    return
  }
  confirmLoading.value = true
  const res = await post('/outp/mzyf/doRefundMaterial', {
    tysl: formatSate.value.tysl,
    remark: formatSate.value.remark,
    yzbh: formatSate.value.id,
    tyshr: props.reviewer
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    confirmLoading.value = false
  } else {
    message.success(res.msg)
    confirmLoading.value = false
    emit('update:open', false)
    emit('success')
  }
}
const onCancel = () => {
  formatSate.value = {}
  emit('update:open', false)
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData.id) {
        formatSate.value = { ...props.rowData }
      }
    }
  }
)
</script>
<style></style>
